﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Controls;
using AppToolkit.WPF.UI;
using NUnit.Framework;

namespace AppToolkit.WPF.Tests.UI
{
    [TestFixture]
    public class PredicateActionTests
    {

        [Test]
        public void Create_from_type()
        {
            DataGrid theGrid = null;
            var pa = PredicateAction.FromType<DataGrid>((s) => theGrid = s);
        }

        [Test,STAThread]
        public void Execute_returns_false_if_no_predicate_matches()
        {
            DataGrid theGrid = null;
            var pa = PredicateAction.FromType<DataGrid>((s) => theGrid = s);

            var ctrl = new Control();

            Assert.IsFalse(pa.Execute(ctrl));
            Assert.IsNull(theGrid);
        }

        [Test, STAThread]
        public void Execute_returns_true_if_predicate_matches()
        {
            DataGrid gridThatWasFound = null;
            var pa = PredicateAction.FromType<DataGrid>((s) => gridThatWasFound = s);

            var theControlToStartSearchFrom = new DataGrid();

            Assert.IsTrue(pa.Execute(theControlToStartSearchFrom));
            Assert.AreEqual(theControlToStartSearchFrom, gridThatWasFound);
        }

        [Test, STAThread]
        public void Execute_returns_true_if_predicate_in_pipe_matches()
        {
            Button buttonThatWasFound = null;
            DataGrid gridThatWasFound = null;

            var pa = PredicateAction.FromType<Button>((b) => buttonThatWasFound = b);
            pa.Append( PredicateAction.FromType<DataGrid>((s) => gridThatWasFound = s));

            var theControlToStartSearchFrom = new DataGrid();

            Assert.IsTrue(pa.Execute(theControlToStartSearchFrom));
            Assert.AreEqual(theControlToStartSearchFrom, gridThatWasFound);
            Assert.IsNull(buttonThatWasFound);
        }
    }
}
